<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a href="javascript:;" :class="[obj.sorter==='weight_desc'?'active':'']" @click="handel('weight_desc')">推荐</a>
      <a href="javascript:;" :class="[obj.sorter===null?'active':'']" @click="handel(null)">最新</a>
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
    </nav>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <ArticleItem
          v-for="item in list"
          :key="item.id"
          :item="item"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { ArticleApi } from '../api/article'
export default {
  name: 'article-page',
  data () {
    return {
      obj: {
        current: 1,
        sorter: null
      },
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
    }
  },
  created () {
    // const res = await ArticleApi(this.obj)
    // console.log(res.data.rows);
    // this.list=res.data.rows
  },
  methods: {
    handel(value){
      this.obj.sorter=value//失效了active切换和高亮效果
      // 重置所有条件
       this.current = 1 // 排序条件变化，重新从第一页开始加载
       this.list = []
       this.finished = false // finished重置，重新有数据可以加载了
        this.loading = true
         // 手动加载更多
  // 手动调用了加载更多，也需要手动将loading改成true，表示正在加载中（避免重复触发）
       this.onLoad()
    },

    onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(async () => {
        if (this.refreshing) {
          this.list = [];
          this.refreshing = false;
        }
        const res = await ArticleApi(this.obj)
        console.log(res.data.rows);
        this.list = [...this.list, ...res.data.rows]
        // 加载状态结束
        this.loading = false;
        this.obj.current += 1

        // 数据全部加载完成
        if (this.list.length >= 100) {
          this.finished = true;
        }
      }, 1000);
    },

     onRefresh() {
      // 清空列表数据
      this.finished = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },

  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>